/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-05 14:42:36
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-05 14:44:49
 * @Description: file content
 */

#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>

int system(const char* cmdstring)
{
    pid_t pid;
    int status;

    if (NULL == cmdstring)
    {
        return 1;
    }

    if ((pid = fork()) < 0)
    {
        status = -1;
    }
    else if (0 == pid)
    {
        execl("/bin/sh", "sh", "-c", cmdstring, (char*)0);
        _exit(127);
    }
    else
    {
        while (waitpid(pid, &status, 0) < 0)
        {
            if (errno != EINR)
            {
                status = -1;
                break;
            }
        }
    }

    return status;
}
